package cn.huixh.blindbox.controller;

import cn.huixh.blindbox.common.ResultData;
import cn.huixh.blindbox.pojo.dto.LoginDTO;
import cn.huixh.blindbox.pojo.vo.LoginVO;
import cn.huixh.blindbox.service.LoginService;
import cn.huixh.blindbox.service.TokenService;
import cn.huixh.blindbox.utils.JwtUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * 登录接口
 *
 * @author huixh
 * @version 1.0
 * @date 2022/5/5 10:48
 */
@Slf4j
@RestController
@RequestMapping("/login")
@RequiredArgsConstructor
public class LoginController {

    final LoginService loginService;

    final JwtUtil jwtUtil;

    final TokenService tokenService;

    /**
     * 登录
     *
     * @param body 登录信息
     * @return 是否登陆成功
     */
    @PostMapping("/login")
    public ResultData< LoginVO > login(@RequestBody LoginDTO body) {
        loginService.login(body.getEmail(), body.getCode());
        String token = tokenService.getToken(body.getEmail());
        return ResultData.success(LoginVO.builder().token(token).messages("用户登陆成功!").build());
    }

    /**
     * 发送用户登录验证码
     *
     * @param email 用户邮箱
     * @return 一般都会返回成功
     */
    @PostMapping("/sendMail")
    public ResultData< String > sendMail(@RequestParam(value = "email") String email) {
        loginService.getLoginCode(email);
        return ResultData.success("发送成功，如果没有收到邮件，请检查垃圾箱！");
    }

}
